const App = getApp();
var template = require('../template/template.js');
Page({

	data: {
		// 页面参数
		options: {},
		// 页面元素
		items: {},
		scrollTop: 0,
		bannerList: [
			"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2979332904,1181156963&fm=26&gp=0.jpg",
			"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1674468107,1858104264&fm=26&gp=0.jpg",
			"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3301582102,974451477&fm=26&gp=0.jpg"
		],
		circular: true,
		//是否显示画板指示点， 根据图片数量自动生成多少个圆点
		indicatorDots: true,
		//选中点的颜色
		//是否竖直
		vertical: false,
		//是否自动切换
		autoplay: false,
		//自动切换的间隔
		interval: 3000,
		//滑动动画时长毫秒
		duration: 1000,
		//所有图片的高度
		imgheights: [],
		//图片宽度
		imgwidth: 320,
		//默认
		current: 0,

		muens: 0,

		searchs: 0,

		//商品列表
		goodslist: [],
		//一级分类
		firstLabel: [],
		//二级分类
		secendThreeLabel: [],
		//banner图片
		bannerList: [],
		name: '',
		indexs: 0,
		goods: [],
		category_id: 0,
		images: []
	},
	onShow: function() {
		let _this = this;
		App._post_form('goods/index', {}, function(res) {
			_this.setData({
				goods: res.data
			});
			_this.goodsDatas(res.data);
		})
		template.tabbar("tabBar", 0, this)//0表示第一个tabbar
	},
	navcato(e){
		wx.redirectTo({
			url: '/'+ e.currentTarget.dataset.urls
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		
	},
	goodsDatas: function(data, ky = 0) {

		let firstgoods, firstLabels = [],
			bannerLists = []
		for (let i = 0; i < data.length; i++) {
			let flabel = {
				category_id: data[i]['category_id'],
				name: data[i]['name']
			}
			firstLabels.push(flabel);
			let banList = {
				image: data[i]['image']
			}
			bannerLists.push(banList);
		}
		
		this.setData({
			goods: data,
			goodslist: data[ky].goods,
			firstLabel: firstLabels,
			bannerList: bannerLists,
			secendThreeLabel: data[ky],
			name: data[ky].name,
			indexs: ky,
			images: data[ky]
		});
	},
	//点击目录内容时取消目录
	bindmenuss: function(e) {
		this.goodsDatas(this.data.goods, e.currentTarget.dataset.bindmenuss)
		this.setData({
			muens: 0,
			current: e.currentTarget.dataset.bindmenuss
		});

	},

	bindmenusst: function(e) {
		this.goodsDatas(this.data.goods, e.currentTarget.dataset.bindmenuss)
		wx.pageScrollTo({
			scrollTop: 0
		})
		this.setData({
			muens: 0,
			current: e.currentTarget.dataset.bindmenuss
		});
	},

	//取消目录
	bindViewCancel() {
		this.setData({
			muens: 0
		});
	},
	//显示目录
	bindViews() {
		this.setData({
			muens: 1
		});

	},
	//显示筛选
	bindsearchs() {
		this.setData({
			searchs: 1,
			muens: 0
		});
	},
	//bindtap="bindsearch"
	bindsearchcli(e) {
		this.setData({
			category_id: e.currentTarget.dataset.ids
		});
	},
	//选中内容之后筛选
	bindsearch(e) {
		this.setData({
			images: this.data.goods[this.data.current]
		});
		let goodsl = [];
		let _this = this;
		App._post_form('goods/lists', {
			category_id: e.currentTarget.dataset.categoryid
		}, function(res) {
			console.log(res)
			let data = res.data.list.data;
			for (let i = 0; i < data.length; i++) {
				let goods = {
					goods_id: data[i]['goods_id'],
					goods_name: data[i]['goods_name'],
					goods_price: data[i]['goods_sku']['goods_price'],
					image: data[i]['goods_image']
				}
				goodsl.push(goods);
			}
			_this.setData({
				searchs: 0,
				goodslist: goodsl,
				category_id: 0
			});
		})
	},

	bindsearchcancel() {
		this.setData({
			searchs: 0
		});
	},



	/**
	 * 分享当前页面
	 */
	onShareAppMessage() {
		let _this = this;
		return {
			title: _this.data.page.params.share_title,
			path: "/pages/index/index?" + App.getShareUrlParams()
		};
	},


	imageLoad(e) { //获取图片真实宽度
		var imgwidth = e.detail.width,
			imgheight = e.detail.height,
			//宽高比
			ratio = imgwidth / imgheight;
		
		//计算的高度值
		var viewHeight = 750 / ratio;
		var imgheight = viewHeight;
		var imgheights = this.data.imgheights;
		//把每一张图片的对应的高度记录到数组里
		imgheights[e.target.dataset.id] = imgheight;
		this.setData({
			imgheights: imgheights
		})
	},

	bindchange(e) {
		let _this = this;
	
		_this.goodsDatas(_this.data.goods, e.detail.current);
		_this.setData({
			current: e.detail.current
		})
		_this.setData({
			images: _this.data.goods[e.detail.current]
		})
	},
	goodsDetail(e) {
		let goodsid = e.currentTarget.dataset.goodsid;
		wx.navigateTo({
			url: "/pages/goods/index?goods_id=" + goodsid
		})
	}


});
